#include "mylinewindows.h"
#include <qlayout.h>
#include <qslider.h>
#include <qcheckbox.h>
#include <qradiobutton.h>
#include "qwt3d_enrichment_std.h"
#include "../../../include/qwt3d_function.h"
#include "qwt3d_surfaceplot.h"
#include <QDebug>
#include <QUdpSocket>
//#include <QtGlobal>
#include<ctime>
using namespace Qwt3D;



class Hat : public Function
{
public:

    Hat(SurfacePlot& pw)
    :Function(pw)
    {
    }

    double operator()(double x, double y)
    {
    double ret = 1.0 / (x*x+y*y+0.5);
    return ret;
    }
    QString name() const {return "$\\frac{1}{x^2+y^2+\\frac{1}{2}}$";}
};

Hat* hat;



mylinewindows::mylinewindows( QWidget* parent )
    : DummyBase( parent )
{


    setupUi(this);
    QGridLayout *grid = new QGridLayout( frame);
    plot = new SurfacePlot(frame);
    grid->addWidget( plot, 0, 0 );

    plot->setTitle("Trace Plot");
    plot->setTitleFont("Arial",12);
    plot->setZoom(0.9);
    plot->setRotation(30,0,15);
    plot->setCoordinateStyle(BOX);

    //Draw Box
    Triple **p=new Triple*[1];
    p[0]=new Triple[7];
    Triple a(0,0,0);
    //zero point
    p[0][0]=a;
    //x-axis
    a.x=-50000;
    a.y=0;
    a.z=0;
    p[0][1]=a;
    a.x=50000;
    a.y=0;
    a.z=0;
    p[0][2]=a;
    //y-axis
    a.x=0;
    a.y=25000;
    a.z=0;
    p[0][3]=a;
    a.x=0;
    a.y=-25000;
    a.z=0;
    p[0][4]=a;
    //z-axis
    a.x=0;
    a.y=0;
    a.z=15000;
    p[0][5]=a;
    a.x=0;
    a.y=0;
    a.z=-15000;
    p[0][6]=a;
    plot->loadFromData(p, 1, 7, true,true);
     for (unsigned i=0; i!=plot->coordinates()->axes.size(); ++i)
   {
     plot->coordinates()->axes[i].setMajors(5);
     plot->coordinates()->axes[i].setMinors(4);
   }
   plot->coordinates()->setGridLinesColor(RGBA(0,0,0));
   plot->coordinates()->setLineWidth(1);
   plot->coordinates()->setNumberFont("Courier",5);
   plot->coordinates()->adjustNumbers(5);
   plot->setCoordinateStyle(BOX);
   plot->coordinates()->setGridLines(true,true,Qwt3D::LEFT|Qwt3D::BACK|Qwt3D::FLOOR);
   //Draw xyz-label
   plot->coordinates()->axes[X1].setLabelString("x-axis");
   plot->coordinates()->axes[Y1].setLabelString("y-axis");
   plot->coordinates()->axes[Z1].setLabelString("z-axis"); // Omega - see http://www.unicode.org/charts/

   plot->updateData();
   plot->updateGL();

   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glDisable(GL_LINE_SMOOTH);



   udpSocket = new QUdpSocket(this);
   //qDebug()<<"1"<<endl;
   udpSocket->bind(8000,QUdpSocket::ShareAddress);
   //qDebug()<<"2"<<endl;
   connect(udpSocket,&QUdpSocket::readyRead,this,&mylinewindows::dealData);
   qDebug()<<"UDP Strat"<<endl;



}
void mylinewindows::AddNewLine(int id)
{
    Qwt3D::Line3D _l3d;
    Qwt3D::Line3D* myLine1 = dynamic_cast<Qwt3D::Line3D *>(plot->addEnrichment(_l3d));
    myLine1->configure(3,true);
    LineList.push_back(myLine1);
    LineList_num.push_back(id);
}
void mylinewindows::JudgeNewLine(int id, Triple point)
{
    int i=0;
    for(i=0;i<LineList_num.size();i++)
    {
        if(id==LineList_num.at(i))
        {
            AddPoint(i,point);
            return;
        }
    }
    AddNewLine(id);
    AddPoint(i,point);
    SetLineColor(i);
}
void mylinewindows::AddPoint(int num, Triple point)
{
    LineList.at(num)->add(point);
}
void mylinewindows::SetLineColor(int num)
{
    srand(0);
    rand();
    rand();
    double r=(double)(rand()%256)/(double)255;
    double g=(double)(rand()%256)/(double)255;
    double b=(double)(rand()%256)/(double)255;
    qDebug()<<r<<endl;
    qDebug()<<g<<endl;
    qDebug()<<b<<endl;
    LineList.at(num)->setLineColor(Qwt3D::RGBA(r,g,b,1));
}
void mylinewindows::UpdateDraw()
{
    plot->updateData();
    plot->updateGL();
}
void mylinewindows::dealData()
{
    //qDebug()<<"4"<<endl;
    char buf[1024]={0};
    QHostAddress cliAddr;
    quint16 port;
    qint64 len=udpSocket->readDatagram(buf,sizeof(buf),&cliAddr,&port);
    //qDebug()<<"5"<<endl;
    if(len>0)
    {

        QString qtstr=QString(QLatin1String(buf));
        QStringList qtstrlist = qtstr.split("\t");
        double datalist[6];
        for(int i=0;i<6;i++)
        {
            datalist[i]=qtstrlist[i].toDouble();
        }
        Triple tr;
        tr.x=datalist[0];
        tr.y=datalist[2];
        tr.z=datalist[4];
        JudgeNewLine(0,tr);
        qDebug()<<qtstr<<endl;
        UpdateDraw();
    }

}
void mylinewindows::resizeEvent(QResizeEvent *e)//使Frame和窗口大小同时变化
{
    int width = this->width();
    int height = this->height();
    frame->setGeometry(0,10,width,height-20);//对角线两个点
}
